{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T07:48:54.189861Z",
     "start_time": "2019-01-19T07:48:54.128270Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy.random import randn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T07:49:20.281232Z",
     "start_time": "2019-01-19T07:49:20.231596Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0: 0.20285576283135517, 1: 0.20461412521887898, 2: -0.3298837961708426, 3: 0.14943443454539743, 4: -0.15762808530284353, 5: -1.1312170213082986, 6: -0.9843606195604201}\n"
     ]
    }
   ],
   "source": [
    "data = {i: randn() for i in range(7)}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T07:51:51.214185Z",
     "start_time": "2019-01-19T07:51:51.169532Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os, sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T07:53:24.886091Z",
     "start_time": "2019-01-19T07:53:24.853336Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.*load*?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T07:56:11.720584Z",
     "start_time": "2019-01-19T07:56:11.654979Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%run ipython_script_test.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T07:56:17.716455Z",
     "start_time": "2019-01-19T07:56:17.691916Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.4666666666666666"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T07:56:23.235532Z",
     "start_time": "2019-01-19T07:56:23.206486Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T09:00:38.643979Z",
     "start_time": "2019-01-19T09:00:38.576444Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "strings = ['foo', 'foobar', 'bza', 'python', 'yangkai', 'fooxx'] * 100000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T09:00:50.468287Z",
     "start_time": "2019-01-19T09:00:50.251955Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 169 ms, sys: 6.8 ms, total: 175 ms\n",
      "Wall time: 180 ms\n"
     ]
    }
   ],
   "source": [
    "time method1 = [x for x in strings if x.startswith('foo')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T09:00:53.613982Z",
     "start_time": "2019-01-19T09:00:53.462532Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 103 ms, sys: 8 ms, total: 111 ms\n",
      "Wall time: 116 ms\n"
     ]
    }
   ],
   "source": [
    "time method2 = [x for x in strings if x[:3] == 'foo']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T09:05:23.842616Z",
     "start_time": "2019-01-19T09:05:18.736280Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 3 µs, sys: 1 µs, total: 4 µs\n",
      "Wall time: 5.96 µs\n"
     ]
    }
   ],
   "source": [
    "%time\n",
    "a = range(1000000)\n",
    "b=335**1000009\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T09:08:24.275696Z",
     "start_time": "2019-01-19T09:08:19.314852Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 24 µs, sys: 152 µs, total: 176 µs\n",
      "Wall time: 180 µs\n",
      "Compiler : 1.22 s\n",
      "4.931442976\n"
     ]
    }
   ],
   "source": [
    "start = time.time()\n",
    "%time b=335**1000009\n",
    "print(time.time()-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T09:03:22.491394Z",
     "start_time": "2019-01-19T09:03:21.955811Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 35 µs, sys: 4 µs, total: 39 µs\n",
      "Wall time: 26 µs\n",
      "Compiler : 107 ms\n"
     ]
    }
   ],
   "source": [
    "%time a=3**1099999"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 性能分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T09:35:51.348082Z",
     "start_time": "2019-01-19T09:35:50.345831Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Laregest one we saw 11.5857762434\n"
     ]
    }
   ],
   "source": [
    "%run cprof_example.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T09:45:01.025408Z",
     "start_time": "2019-01-19T09:45:00.992140Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['%prun', '%rerun', '%run', '%%prun']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = %lsmagic\n",
    "a = str(a)\n",
    "a = a.split()\n",
    "[x for x in a if 'run' in x]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-20T05:31:00.565896Z",
     "start_time": "2019-01-20T05:30:57.770366Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Laregest one we saw 11.3828138112\n",
      "111224\n",
      "Wrote profile results to cprof_example.py.lprof\n",
      "Timer unit: 1e-06 s\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!kernprof -l cprof_example.py\n",
    "!python -m line_profiler cprof_example.py.lprof  # 请加上@profile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-20T05:30:14.807748Z",
     "start_time": "2019-01-20T05:30:14.048716Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Laregest one we saw 11.7159842493\n",
      "111224\n"
     ]
    }
   ],
   "source": [
    "from cprof_example import ax, run_experiment, fun"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T10:03:34.818017Z",
     "start_time": "2019-01-19T10:03:34.789240Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "111224\n"
     ]
    }
   ],
   "source": [
    "run cprof_example.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T10:03:36.497696Z",
     "start_time": "2019-01-19T10:03:36.468523Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-19T10:04:28.142573Z",
     "start_time": "2019-01-19T10:04:28.118011Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "fun()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-20T05:31:05.347214Z",
     "start_time": "2019-01-20T05:31:05.321601Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The line_profiler extension is already loaded. To reload it, use:\n",
      "  %reload_ext line_profiler\n"
     ]
    }
   ],
   "source": [
    "%load_ext line_profiler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-20T05:31:12.799055Z",
     "start_time": "2019-01-20T05:31:12.283939Z"
    }
   },
   "outputs": [],
   "source": [
    "%lprun -f run_experiment run_experiment()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
